import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xim/common/model/file_type.dart';
import 'package:xim/common/module/qr.dart';
import 'package:xim/config/binding_config.dart';
import 'package:xim/config/store_config.dart';
import 'package:xim/http/apis/user/user_model.dart';
import 'package:xim/router/index.dart';
import 'package:xim/screen/chat_book_screen/page/new_friends/new_friends_binding.dart';
import 'package:xim/screen/chat_book_screen/page/new_friends/new_friends_page.dart';
import 'package:xim/screen/guide_page/guide_page.dart';
import 'package:xim/screen/login_screen/account_login_page/account_login_binding.dart';
import 'package:xim/screen/login_screen/account_login_page/account_login_page.dart';
import 'package:xim/screen/login_screen/fingerprint_login_page.dart';
import 'package:xim/screen/login_screen/login_guide_page/login_guide_page.dart';
import 'package:xim/screen/login_screen/register_page/register_page.dart';
import 'package:xim/screen/login_screen/register_page/register_page_binding.dart';
import 'package:xim/screen/login_screen/welcome_login_page/welcome_login_binding.dart';
import 'package:xim/screen/main_screen.dart';
import 'package:xim/screen/message_list_screen/page/add_friend_page.dart';
import 'package:xim/screen/message_list_screen/page/add_friend_search_page.dart';
import 'package:xim/screen/message_list_screen/page/chat_detail_page/chat_detail_binding.dart';
import 'package:xim/screen/message_list_screen/page/chat_detail_page/chat_detail_page.dart';
import 'package:xim/screen/message_list_screen/page/message_detail_info_page/chat_message_detail.dart';
import 'package:xim/screen/message_list_screen/page/chat_room_info_page.dart';
import 'package:xim/screen/message_list_screen/page/message_detail_info_page/message_detail_binding.dart';
import 'package:xim/screen/message_list_screen/page/session_create_page/create_session_page.dart';
import 'package:xim/screen/message_list_screen/page/session_create_page/session_create_binding.dart';
import 'package:xim/screen/message_list_screen/page/session_secretkey_detail_page.dart';
import 'package:xim/screen/page/file_view_page.dart';
import 'package:xim/screen/page/pdf_view_page.dart';
import 'package:xim/screen/page/photo_view_page.dart';
import 'package:xim/screen/page/qr_page.dart';
import 'package:xim/screen/page/qr_scan_page.dart';
import 'package:xim/screen/page/video_view_h5_page/video_view_h5_binding.dart';
import 'package:xim/screen/page/video_view_h5_page/video_view_h5_page.dart';
import 'package:xim/screen/page/video_view_page.dart';
import 'package:xim/screen/page/video_view_page/video_view_binding.dart';
import 'package:xim/screen/page/video_view_page/video_view_page.dart';
import 'package:xim/screen/page/web_view_page.dart';
import 'package:xim/screen/profile_screen/page/control_page.dart';
import 'package:xim/screen/profile_screen/page/detail_profile_info_page.dart';
import 'package:xim/screen/profile_screen/page/login_setting.dart';
import 'package:xim/screen/profile_screen/page/profile_public_page/profile_public_binding.dart';
import 'package:xim/screen/profile_screen/page/profile_public_page/profile_public_page.dart';

import '../screen/login_screen/login_page/login_page.dart';
import '../screen/login_screen/welcome_login_page/welcome_login.dart';


/// 路由配置
class RouteConfig {

  static List<GetPage> get route => [
    GetPage(name: Routers.home, page: ()=>const MainScreen(), ),
    GetPage(name: Routers.login, page: ()=>LoginScreen(),),
    GetPage(
      name: Routers.chat,
      page: (){
        var id = Get.parameters['id'] as int;
        return ChatDetailPage(sessionId: id,);
      },
    ),
    GetPage(
      name: Routers.guide,
      page: (){
        return const GuidePage();
      },
    )
  ];

}

class Routers {
  static const home = "/home";
  static const login = "/login";
  static const loginGuide = "/login/guide";
  static const pwdLogin = "/login/pwd";
  static const fingerprintLogin = "/login/fingerprint";
  static const chat = "/chat/session";
  static const guide = "/guide";

  static toHome({RouteType type=RouteType.push}){
    if(type==RouteType.replace){
      Get.off(const MainScreen(), binding: BindingsConfig());
    } else if(type==RouteType.replaceAll){
      Get.offAll(const MainScreen(), binding: BindingsConfig());
    } else{
      Get.to(const MainScreen(), binding: BindingsConfig());
    }
  }

  static toLogin(){
    Get.to(LoginScreen());
  }

  static toFingerprintLogin(UserInfoModel user){
    Get.to(FingerprintLoginPage(user: user,));
  }

  static toChat(int sessionId){
    Get.to(
      ChatDetailPage(sessionId: sessionId,),
      binding: ChatDetailBinding(sessionId),
    );
  }

  static toMessageDetailPage(int messageId){
    Get.to(
      ChatMessageDetailPage(messageId,),
      binding: MessageDetailBinding(messageId)
    );
  }

  static toMediaView(MediaView mediaView){
    late Widget page;
    switch(mediaView.fileType){
      case FileType.image:
        page = PhotoViewPage(mediaView: mediaView,);
        break;
      case FileType.video:
        Get.to(VideoViewH5Page(), binding: VideoViewH5Binding(mediaView.gfsName, mediaView.plaintextAESKey));
        return;
      case FileType.pdf:
        page = PdfViewPage(mediaView: mediaView,);
        break;
      default:
        page = FileViewPage(mediaView: mediaView,);
        break;
    }
    Get.to(page);
  }

  static toChatInfo(int sessionId){
    Get.to(ChatRoomInfoPage(sessionId: sessionId));
  }

  static toGuidePage(){
    Get.to(const GuidePage());
  }

  static toLoginGuidePage({RouteType? type}) async {
    if(type==RouteType.replace){
      Get.off( LoginGuidePage());
    } else if(type==RouteType.replaceAll){
      Get.offAll(LoginGuidePage());
    } else{
      Get.to(LoginGuidePage());
    }
    Get.to(LoginGuidePage());
  }

  static toRegisterPage(){
    Get.to(RegisterPage(), binding: RegisterPageBinding());
  }

  static toWelcomeLoginPage({RouteType type=RouteType.replaceAll}){
    if(type==RouteType.replace){
      Get.off( WelcomeLoginPage(), binding: WelcomeLoginBindings());
    } else if(type==RouteType.replaceAll){
      Get.offAll( WelcomeLoginPage(), binding: WelcomeLoginBindings());
    } else{
      Get.to( WelcomeLoginPage(), binding: WelcomeLoginBindings());
    }
  }

  static void back(){
    Get.back();
  }

  static void toAccountLoginPage(){
    Get.to(AccountLoginPage(), binding: AccountLoginBinding());
  }

  static toScanPage() {
    Get.to(const QrScanPage());
  }

  static toFriendAddPage() {
    Get.to(const AddFriendPage());
  }

  static toCreateSessionPage() {
    Get.to(SessionCreatePage(), binding: SessionCreateBinding());
  }

  static toWebViewPage(String uri, {RouteType type=RouteType.replace}){
    if(type==RouteType.replace){
      Get.off(WebViewPage(content: uri));
    } else if(type==RouteType.replaceAll){
      Get.offAll(WebViewPage(content: uri));
    } else{
      Get.to(WebViewPage(content: uri));
    }
  }

  static toAddFriendSearchPage(){
    Get.to(const AddFriendSearchPage());
  }

  static toQrPage(IQrResult qrData){
    Get.to(UserQrPage(qrData: qrData,));
  }

  static void toProfilePublicPage(String account) {
    Get.to(
      ProfilePublicPage(userAccount: account,),
      binding: ProfilePublicBinding(account)
    );
  }

  static void toDetailProfileInfoPage(String account) {
    Get.to(DetailProfileInfoPage(account:account));
  }

  static void toSessionSecurityKeyDetailPage(int securityKeyId){
    Get.to(SessionSecretKeyDetailPage(secretKeyId: securityKeyId,));
  }

  static void toLoginSettingPage(){
    Get.to(const LoginSetting());
  }

  static void toControlPage(){
    Get.to(ControlPage());
  }

  /// 新朋友页面
  static void toNewFriendsPage(){
    Get.to(NewFriendsPage(), binding: NewFriendsBinding());
  }

}

enum RouteType {
  push,
  replace,
  replaceAll,
 ;
}